package com.kehao.newcoder;

import org.junit.Test;

public class NC5Solution {


    private class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    int result = 0;

    public int sumNumbers(TreeNode root) {
        // write code here
        result = 0;
        dfs(root, 0);
        return result;
    }

    private void dfs(TreeNode root, int num) {
        if(root==null){
            return;
        }
        num = num * 10 + root.val;
        if(root.left!=null){
            dfs(root.left, num);
        }
        if(root.right!=null){
            dfs(root.right, num);
        }
        if(root.left==null&&root.right==null){
            result+=num;
        }
    }

    @Test
    public void test01() {
        TreeNode root = new TreeNode(1);
//        root.left = new TreeNode(2);
        root.right = new TreeNode(5);
        int result = sumNumbers(root);
        System.out.println(result);
    }
}
